﻿namespace YunQue.Core.Common.Extensions
{
    /// <summary>
    /// list 扩展
    /// </summary>
    public static class ListExtensions
    {
        /// <summary>
        /// 判断List是否为空或者null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty<T>(this List<T> list)
        {
            if (list != null)
            {
                return list.Count == 0;
            }
            return true;
        }
        /// <summary>
        /// 判断List是否不为空或者null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool IsNotNullOrEmpty<T>(this List<T> list)
        {
            return !list.IsNullOrEmpty();
        }
        /// <summary>
        /// 分割List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<List<T>> Split<T>(this List<T> list,int count)
        {
            if (list == null || count <= 0)
            {
                throw new ArgumentException("List不能为空且分割数量必须大于0");
            }
            if(list.Count <= count)
            {
                return new List<List<T>> { list };
            }
            List<List<T>> result = new List<List<T>>();
            for(int i = 0; i < list.Count; i += count)
            {
                result.Add(list.GetRange(i, Math.Min(count, list.Count - i)));
            }
            return result;
        }

    }
}
